package cn.jn.lhm.order.service.impl;

import cn.jn.lhm.order.client.EduOrderClient;
import cn.jn.lhm.order.client.UcOrderClient;
import cn.jn.lhm.order.entity.Order;
import cn.jn.lhm.order.mapper.OrderMapper;
import cn.jn.lhm.order.service.OrderService;
import cn.jn.lhm.order.utils.OrderNumUtil;
import cn.jn.lhm.utils.order.EduOrder;
import cn.jn.lhm.utils.order.UcOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/15 - 20:02
 */
@Service
public class OrderServiceImpl  implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private EduOrderClient eduOrderClient;

    @Resource
    private UcOrderClient ucOrderClient;

    @Override
    public String createOrder(String courseId, String userId) {

        EduOrder courseInfoToOrder = eduOrderClient.getCourseInfoToOrder(courseId);

        UcOrder userInfoByUserId = ucOrderClient.getUserInfoByUserId(userId);

        Order order = new Order();

        order.setOrderNo(OrderNumUtil.getOrderNo())
                .setCourseId(courseId)
                .setCourseCover(courseInfoToOrder.getCover())
                .setCourseTitle(courseInfoToOrder.getTitle())
                .setTeacherName(courseInfoToOrder.getTeacherName())
                .setTotalFee(courseInfoToOrder.getPrice())
                .setMemberId(userId)
                .setMobile(userInfoByUserId.getMobile())
                .setNickname(userInfoByUserId.getNickname())
                .setStatus(0)
                .setPayType(1)
                ;
        orderMapper.insert(order);
        return order.getOrderNo();
    }

    @Override
    public Order getOrderInfoByOrderNum(String id) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", id);
        return orderMapper.selectOne(wrapper);
    }
}
